package com.aistar.service.impl;

import com.aistar.mapper.QuestionDetailMapper;
import com.aistar.pojo.Choice;
import com.aistar.pojo.QuestionDetail;
import com.aistar.pojo.QuestionDetailExample;
import com.aistar.pojo.QuestionSet;
import com.aistar.service.QuestionDetailService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionDetailServiceImpl implements QuestionDetailService {

    @Autowired
    private QuestionDetailMapper questionDetailMapper;

    @Override
    public ServerResponse add(QuestionDetail questionDetail) {
        int rows=questionDetailMapper.insertSelective(questionDetail);
        if(rows>0)
            return ServerResponse.addSuccess();
        return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse getAnswer(Integer questionDetailId) {
        QuestionDetail questionDetail=questionDetailMapper.selectByPrimaryKey(questionDetailId);
        Integer choiceId=questionDetail.getChoiceId();
        Choice choice=new Choice();
        choice.setChoiceId(choiceId);
        String answer=choice.getChoiceCorrectAnswer();
        if(!answer.equals(""))
            return ServerResponse.getSuccess(answer);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getById(Integer questionDetailId) {
        QuestionDetail questionDetail=questionDetailMapper.selectByPrimaryKey(questionDetailId);
        if(questionDetail!=null)
            return ServerResponse.getSuccess(questionDetail);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByQuestionSetId(Integer questionSetId) {
        QuestionDetailExample example=new QuestionDetailExample();
        example.createCriteria().andQuestionSetIdEqualTo(questionSetId);
        List<QuestionDetail> questionDetailList=questionDetailMapper.selectByExample(example);
        if(questionDetailList!=null && questionDetailList.size()>0)
            return ServerResponse.getSuccess(questionDetailList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse modify(QuestionDetail questionDetail) {
        int rows=questionDetailMapper.updateByPrimaryKeySelective(questionDetail);
        if(rows>0)
            return ServerResponse.modifySuccess();
        return ServerResponse.modifyFailed();
    }

    @Override
    public ServerResponse getByQuestionSetIdAndChoiceId(Integer questionSetId, Integer choiceId) {
        QuestionDetailExample example=new QuestionDetailExample();
        example.createCriteria().andQuestionSetIdEqualTo(questionSetId)
                .andChoiceIdEqualTo(choiceId);
        List<QuestionDetail> questionDetailList=questionDetailMapper.selectByExample(example);
        if(questionDetailList !=null && questionDetailList.size()>0)
            return ServerResponse.getSuccess(questionDetailList.get(0));
        return ServerResponse.getFailed();
    }


}
